import { reactive } from 'vue'
import { addMenu, updateMenu, getMenuOptionList, getMenuForm, deleteMenu } from '@/api/system/menu'
import { message } from 'ant-design-vue'
export function useDetail(getFormValues: Function) {
  const status = reactive({
    saving: false,
    loading: false
  })
  const createOrUpdate = async () => {
    const values = getFormValues()
    if (status.saving) return false
    status.saving = true
    try {
      values?.code ? await updateMenu(values.code, values) : await addMenu(values)
      status.saving = false
      values?.code ? message.info('菜单更新成功！') : message.info('菜单新增成功！')
      return true
    } catch (err) {
      status.saving = false
      return false
    }
  }

  const getMenuAllList = async () => {
    return getMenuOptionList(false)
  }

  const getCatalogAndMenuList = async () => {
    return getMenuOptionList(true)
  }

  const getMenuById = async (code: any) => {
    return await getMenuForm(code)
  }

  const deleteMenuById = async (code: any) => {
    await deleteMenu(code)
  }
  return { status, createOrUpdate, getMenuAllList, getCatalogAndMenuList, getMenuById, deleteMenuById }
}
